﻿Imports System
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Class MainForm

    Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click
        Dim regexNumbers As New System.Text.RegularExpressions.Regex("\d")
        For Each item In CardsTextBox.Lines
            Dim strCard As String = regexNumbers.Replace(item.Trim().Replace("x2", ""), "")
            If strCard = "" Or strCard = "Mage ()" Or strCard = "Hunter ()" Or strCard = "Priest ()" Or strCard = "Warlock ()" Or strCard = "Neutral ()" _
                Or strCard = "Paladin ()" Or strCard = "Druid ()" Or strCard = "Warrior ()" Or strCard = "Rogue ()" Then Continue For
            If item.Contains("x2") Then DeckListBox.Items.Add(strCard)
            DeckListBox.Items.Add(strCard)
        Next
        CardsTextBox.Clear()
    End Sub

    Private Sub DeckListBox_KeyDown(sender As Object, e As KeyEventArgs) Handles DeckListBox.KeyDown

        If e.KeyCode = Keys.Delete Then
            Dim intSelected As Integer = DeckListBox.SelectedIndex
            DeckListBox.Items.RemoveAt(intSelected)
            DeckListBox.SelectedIndex = intSelected
        End If

    End Sub

    Private Sub ClearButton_Click(sender As Object, e As EventArgs) Handles ClearButton.Click
        DeckListBox.Items.Clear()
    End Sub

    Private Sub BuildButton_Click(sender As Object, e As EventArgs) Handles BuildButton.Click

        If DeckListBox.Items.Count = 0 Then
            MsgBox("Deck is empty!")
            Exit Sub
        End If

        Dim hwndHearth As IntPtr = FindWindow("UnityWndClass", "Hearthstone")
        If hwndHearth = IntPtr.Zero Then
            MsgBox("Hearthstone is not running!")
            Return
        Else
            SetForegroundWindow(hwndHearth)
        End If

        Threading.Thread.Sleep(750)

        Dim lastCard As String = Nothing
        For Each item In DeckListBox.Items
            If lastCard = item Then
                CardMacro(item, True)
            Else
                CardMacro(item, False)
            End If
            lastCard = item
        Next

        SetForegroundWindow(Me.Handle)

        MsgBox("Build Complete!")

    End Sub

    Private Sub CardMacro(ByVal Card As String, ByVal blnSecond As Boolean)

        If Not blnSecond Then

            Cursor.Position = New Point(950, 1000)
            mouse_event(&H2, 0, 0, 0, 0)
            mouse_event(&H4, 0, 0, 0, 0)

            Threading.Thread.Sleep(200)

            SendKeys.Send(Card)
            SendKeys.Send("{Enter}")

            Threading.Thread.Sleep(200)

        End If

        Cursor.Position = New Point(425, 350)
        mouse_event(&H2, 0, 0, 0, 0)
        mouse_event(&H4, 0, 0, 0, 0)

        Threading.Thread.Sleep(200)

    End Sub

    Declare Auto Function FindWindow Lib "USER32.DLL" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

    Declare Auto Function SetForegroundWindow Lib "USER32.DLL" _
    (ByVal hWnd As IntPtr) As Integer

    Declare Sub mouse_event Lib "user32" _
    (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)

End Class
